import 'package:flutter/material.dart';

import '../../config/kq_global.dart';
import '../../resources/images.dart';
import '../../resources/kq_theme_colors.dart';
import '../../resources/l10n/kq_intl.dart';
import '../../theme/kq_theme_manager.dart';
import '../button/kq_ink_well.dart';

/// 操作类对话框
///
/// @author 周卓
///
/// 顶部标题和关闭按钮 + 中间自定义 + 底部1个蓝色按钮
class KqPadOptDialog extends AlertDialog {
  /// 标题
  final String? titleString;

  /// 自定义控件
  final Widget child;

  /// 按钮文字，默认"确定"
  final String? btnText;

  /// 自定义对话框宽度，默认420
  final double? width;

  /// 自定义对话框高度，默认自适应
  final double? height;

  /// 按钮回调
  final Function()? onBtnTap;

  const KqPadOptDialog(
      {Key? key,
      required this.child,
      this.titleString,
      this.width,
      this.height,
      this.btnText,
      this.onBtnTap})
      : super(key: key);

  @override
  Widget build(BuildContext context) {
    final List<Widget> children = <Widget>[];

    /// 标题
    children.add(Stack(
      children: [
        Container(
          padding: EdgeInsets.symmetric(horizontal: 48, vertical: 16),
          alignment: Alignment.center,
          child: Text(
            titleString ?? '',
            style: TextStyle(
                fontSize: 16,
                color: KqThemeColors.text26,
                fontWeight: FontWeight.bold),
          ),
        ),
        Positioned(
          right: 8,
          top: 8,
          height: 32,
          width: 32,
          child: KqInkWell(
            onTap: () {
              Navigator.of(context).pop();
            },
            verticalPadding: 8,
            horizontalPadding: 8,
            radius: 200,
            child: Image.asset(
              Images.commonIcCloseDialog16,
              width: 16,
              height: 16,
              fit: BoxFit.fill,
              package: KqGlobal.packageName,
            ),
          ),
        )
      ],
    ));

    /// 消息
    children.add(Padding(
      padding: EdgeInsets.symmetric(horizontal: 32),
      child: child,
    ));

    /// 下边距
    children.add(SizedBox(
      width: 1,
      height: 16,
    ));

    /// 底部按钮
    children.add(Row(
      children: [
        Expanded(
            child: Align(
          child: SizedBox(
            width: 200,
            child: KqInkWell(
              onTap: () {
                if (onBtnTap != null) {
                  onBtnTap!();
                }
              },
              radius: 1,
              backgroundColor: KqThemeManager.getCommonConfig().mainColor ??
                  KqThemeColors.bgBlue,
              child: Container(
                alignment: Alignment.center,
                height: 40,
                child: Text(
                  btnText ?? KqIntl.currentResource.confirm,
                  maxLines: 1,
                  overflow: TextOverflow.ellipsis,
                  style:
                      TextStyle(fontSize: 16, color: KqThemeColors.textWhite),
                ),
              ),
            ),
          ),
        ))
      ],
    ));

    /// 下边距
    children.add(SizedBox(
      width: 1,
      height: 16,
    ));

    Widget dialogChild = Column(
      mainAxisSize: MainAxisSize.min,
      crossAxisAlignment: CrossAxisAlignment.stretch,
      children: children,
    );
    var padding = MediaQuery.of(context).viewInsets.bottom;
    return UnconstrainedBox(
        child: Container(
            padding: EdgeInsets.only(bottom: padding / 2),
            width: width ?? 420,
            height: height,
            child: Material(
                shape: RoundedRectangleBorder(
                    borderRadius: BorderRadius.all(Radius.circular(8))),
                color: KqThemeColors.bgWhite,
                child: ClipRRect(
                  borderRadius: BorderRadius.all(Radius.circular(8)),
                  child: dialogChild,
                ))));
  }
}
